/* enum */
import { TaskFieldNameMappingEnum } from '@model/enum/FieldMappingEnum'
import { QualityInfoStateValueEnum } from '@model/enum/QualityInfoEnum'
/* entity */
import Field from '@model/entity/Field'
/* service */
import { getField } from '@service/FieldService'
import { isQualityStatusIn } from '@service/QualityInfoService'

export function getTaskQualityField(fields: Field[]): Field | null {
  return getField(TaskFieldNameMappingEnum.Quality, fields)
}

export function getTaskQualityFieldIsFreeServiceFee(field: Field): boolean {
  return Boolean(field?.setting?.isFreeServiceFee)
}

export function getTaskQualityFieldIsFreeSparePartFee(field: Field): boolean {
  return Boolean(field?.setting?.isFreeSparePartFee)
}

export function getTaskDefaultQuality(field: Field): string | undefined {
  return field?.setting?.defaultQuality
}

export function isQualityTask(task: Record<string, any>): boolean {
  // 不是只有一个产品的工单不符合质保要求
  if (!isOnlyOneProductInTask(task)) return false
  
  // 产品信息
  const product: Record<string, any> = task?.products?.[0] || {}
  // 质保状态
  const qualityStatus: string = product?.qualityStatus || ''
  
  return isQualityStatusIn(qualityStatus)
}

function isOnlyOneProductInTask(task: Record<string, any>): boolean {
  return task?.products?.length === 1
}